{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import jieba\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import MultinomialNB  # 特征：词的次数\n",
    "import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>内容</th>\n",
       "      <th>评价</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>从编程小白的角度看，入门极佳。</td>\n",
       "      <td>好评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>很好的入门书，简洁全面，适合小白。</td>\n",
       "      <td>好评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>讲解全面，许多小细节都有顾及，三个小项目受益匪浅。</td>\n",
       "      <td>好评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>前半部分讲概念深入浅出，要言不烦，很赞</td>\n",
       "      <td>好评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>看了一遍还是不会写，有个概念而已</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>中规中矩的教科书，零基础的看了依旧看不懂</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>内容太浅显，个人认为不适合有其它语言编程基础的人</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>破书一本</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>适合完完全全的小白读，有其他语言经验的可以去看别的书</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>基础知识写的挺好的！</td>\n",
       "      <td>好评</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          内容   评价\n",
       "0             从编程小白的角度看，入门极佳。  好评\n",
       "1           很好的入门书，简洁全面，适合小白。  好评\n",
       "2   讲解全面，许多小细节都有顾及，三个小项目受益匪浅。  好评\n",
       "3         前半部分讲概念深入浅出，要言不烦，很赞  好评\n",
       "4            看了一遍还是不会写，有个概念而已  差评\n",
       "5        中规中矩的教科书，零基础的看了依旧看不懂  差评\n",
       "6    内容太浅显，个人认为不适合有其它语言编程基础的人  差评\n",
       "7                        破书一本  差评\n",
       "8  适合完完全全的小白读，有其他语言经验的可以去看别的书  差评\n",
       "9                  基础知识写的挺好的！  好评"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"数据评论.csv\",\n",
    "                   encoding='ANSI')\n",
    "\n",
    "train = data.head(10).copy()  # 前10条训练\n",
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 文本内容进行分词\n",
    "def split_text(val):\n",
    "    return \" \".join(list(jieba.cut(val)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    从       编程       小白       的       角度看       ， ...\n",
       "1    很       好       的       入门       书       ，    ...\n",
       "2    讲解       全面       ，       许多       小       细节 ...\n",
       "3    前半部       分讲       概念       深入浅出       ，      ...\n",
       "4    看       了       一遍       还是       不会       写  ...\n",
       "5    中规中矩       的       教科书       ，       零       基...\n",
       "6    内容       太       浅显       ，       个人       认为 ...\n",
       "7                                          破书       一本\n",
       "8    适合       完完全全       的       小白读       ，       ...\n",
       "9    基础知识       写       的       挺       好       的  ...\n",
       "Name: 内容 , dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[\"内容 \"] = train[\"内容 \"].transform(split_text)\n",
    "train[\"内容 \"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征名称 ['一本', '一遍', '三个', '中规中矩', '依旧', '入门', '内容', '分讲', '前半部', '受益匪浅', '基础', '基础知识', '完完全全', '小白', '小白读', '很赞', '教科书', '有个', '极佳', '概念', '浅显', '深入浅出', '看不懂', '破书', '简洁', '细节', '经验', '编程', '要言不烦', '角度看', '讲解', '语言', '适合', '项目', '顾及']\n",
      "[[0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0]\n",
      " [0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0]\n",
      " [0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1]\n",
      " [0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0]\n",
      " [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
      " [0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0]\n",
      " [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0]\n",
      " [0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "# 2. 词频向量化\n",
    "\n",
    "# 加载停用词\n",
    "stop_words = []\n",
    "with open(\"stopWord.txt\", encoding='utf-8') as f:\n",
    "    for line in f.readlines():\n",
    "        # print(line.strip())\n",
    "        stop_words.append(line.strip())\n",
    "\n",
    "cnt = CountVectorizer(stop_words=stop_words)\n",
    "cnt.fit(train[\"内容 \"])  # 找出词的特征名称\n",
    "\n",
    "X_train = cnt.transform(train[\"内容 \"]).toarray()  # 训练集的特征\n",
    "print(\"特征名称\", cnt.get_feature_names())\n",
    "print(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultinomialNB()"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train = train[\"评价\"]\n",
    "\n",
    "# print(\"训练集特征\\n\", X_train, X_train.shape)\n",
    "# print(\"训练集标签\\n\", y_train, y_train.shape)\n",
    "\n",
    "\n",
    "nb = MultinomialNB()\n",
    "\n",
    "nb.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>内容</th>\n",
       "      <th>评价</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>太基础</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>略啰嗦。。适合完全没有编程经验的小白</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>真的真的不建议买</td>\n",
       "      <td>差评</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   内容   评价\n",
       "10                 太基础  差评\n",
       "11  略啰嗦。。适合完全没有编程经验的小白  差评\n",
       "12            真的真的不建议买  差评"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = data.tail(3)  # 后3条\n",
    "test_org = test.copy()\n",
    "test_org"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集特征\n",
      " [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "X_test = cnt.transform(test[\"内容 \"]).toarray()\n",
    "\n",
    "print(\"测试集特征\\n\", X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测结果\n",
      " ['差评' '差评' '好评']\n",
      "真实结果\n",
      " ['差评', '差评', '差评']\n",
      "预测概率\n",
      " [[0.24669604 0.75330396]\n",
      " [0.48230781 0.51769219]\n",
      " [0.5        0.5       ]]\n"
     ]
    }
   ],
   "source": [
    "y_pred = nb.predict(X_test)\n",
    "print(\"预测结果\\n\", y_pred)\n",
    "print(\"真实结果\\n\", test[\"评价\"].tolist())\n",
    "print(\"预测概率\\n\", nb.predict_proba(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
